package cn.zwxky.android.user.My;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sobot.chat.SobotApi;
import com.sobot.chat.api.model.Information;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbFragment;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInjectUtils;
import com.zgscwjm.lsfbbasetemplate.config.Config;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;

import java.util.HashMap;

import cn.zwxky.android.user.R;
import cn.zwxky.android.user.Utils.LittleUtils;
import cn.zwxky.android.user.Utils.Permission;
import cn.zwxky.android.user.Utils.URLString;


/**
 * Created by Administrator on 2017/3/15 0015.
 */

public class MyFragment extends LsfbFragment {


    /**
     * view 对象
     */
    private View view;

    /**
     * activity对象
     */
    private Activity mactivity;

    @ViewInject(R.id.tv_badg)
    private TextView tv_badg;

    /**
     * 昵称
     */
    @ViewInject(R.id.fg_text_nikename)
    private TextView text_nikename;

    @ViewInject(R.id.fg_my_img_head)
    private SimpleDraweeView img_head;

    private Information info;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, null);
        ViewInjectUtils.inject(this, view);
        LsfbEvent.getInstantiation().register2(this);
        Permission permission = new Permission(mactivity);
        permission.GetMeshPermission();
        permission.GetAddressBookPermission();
        permission.GetLocationPermission2();
        info = new Information();

//        if (LittleUtils.badg.isEmpty() || LittleUtils.badg.equals("0")){
//            tv_badg.setVisibility(View.GONE);
//        }else {
//            tv_badg.setVisibility(View.VISIBLE);
//            tv_badg.setText(LittleUtils.badg);
//        }
        getData();
        return view;
    }

    /**
     * 用于存储activity对象
     *
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mactivity = activity;
    }


    @Override
    public void onStart() {
        super.onStart();
        //获取未读消息数并显示
        String unreadNum = SobotApi.getUnreadMsg(mactivity.getApplicationContext()) > 0 ?
                SobotApi.getUnreadMsg(mactivity.getApplicationContext()) + "" : "";
        LsfbLog.e("获取未读消息数并显示"+unreadNum);
        if (unreadNum.isEmpty()){
            tv_badg.setVisibility(View.GONE);
        }else {
            tv_badg.setVisibility(View.VISIBLE);
            tv_badg.setText(unreadNum);
        }
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.fg_msg, R.id.fg_my_details, R.id.fg_pwd, R.id.fg_system_msg, R.id.fg_setting})
    public void onClick(View view) {
        switch (view.getId()) {
            //消息
            case R.id.fg_msg:
                info.setAppkey("a14627c9117f47da9ab858fc55083aef");
                info.setUid(Config.getUserId());
                info.setUname(LittleUtils.nikename);
                info.setFace(LittleUtils.img_head);
                info.setTel(LittleUtils.phone);
                info.setColor("#374164");
                SobotApi.startSobotChat(mactivity, info);
                break;
            //资料管理
            case R.id.fg_my_details:
                startActivity(new Intent(mactivity, MyDetailsActivity.class));
                break;
            //密码管理
            case R.id.fg_pwd:
                startActivity(new Intent(mactivity, PwdActivity.class));
                break;
            //系统消息
            case R.id.fg_system_msg:
                startActivity(new Intent(mactivity, SystemMsgActivity.class));
                break;
            //设置
            case R.id.fg_setting:
                startActivity(new Intent(mactivity, SettingActivity.class));
                break;
        }
    }




    @Override
    protected void Resume() {
        getData();
        //获取未读消息数并显示
        String unreadNum = SobotApi.getUnreadMsg(mactivity.getApplicationContext()) > 0 ?
                SobotApi.getUnreadMsg(mactivity.getApplicationContext()) + "" : "";
        LsfbLog.e("获取未读消息数并显示"+unreadNum);
        if (unreadNum.isEmpty()){
            tv_badg.setVisibility(View.GONE);
        }else {
            tv_badg.setVisibility(View.VISIBLE);
            tv_badg.setText(unreadNum);
        }
    }

    @Override
    public void initData() {

    }

    public void getData() {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("uid", Config.getUserId());
        new BaseInternet().getData_Get(URLString.INFO, hashMap, MyDetailsBean.class, new MyDetailsEvent<MyDetailsBean>(), true, true);
    }

    @EventAnnotation
    public void MyDetailsEvent(MyDetailsEvent<MyDetailsBean> event) {
        LsfbLog.e("dsdas");
        switch (event.getCode()) {
            case 200:
                if (!event.getData().getImg().isEmpty()) {
                    img_head.setImageURI(Uri.parse(URLString.IMG_URL + event.getData().getImg()));
                } else {
                    img_head.setBackgroundResource(R.mipmap.img_my_head);
                }
                text_nikename.setText(event.getData().getNickname());
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        LsfbEvent.getInstantiation().unregister2(this);
        super.onDestroy();
    }

}
